package com.runtu.domain.activity.service.trial.node;

import com.runtu.domain.activity.model.entity.MarketProductEntity;
import com.runtu.domain.activity.model.entity.TrialBalanceEntity;
import com.runtu.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.runtu.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import com.runtu.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import com.runtu.types.design.framework.tree.StrategyHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName TagNode
 * @Description 人群标签节点
 * @date 2025/7/24 22:15
 * @Version 1.0
 */
@Slf4j
@Service
public class TagNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    @Resource
    private EndNode endNode;

    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        //获取活动额配置信息
        GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();

        //人群标签的id
        String tagId = groupBuyActivityDiscountVO.getTagId();
        //人群标签的作用范围
        String tagScope = groupBuyActivityDiscountVO.getTagScope();
        /*按道理来说 我们应该在这里写实现方法 获取人群标签的范围 然后判断当前的用户是否属于 但是复用性就变差了 所以选择写道GroupBuyActivityDiscountVO中 提高复用性*/
        boolean visible = groupBuyActivityDiscountVO.isVisible();
        boolean enable = groupBuyActivityDiscountVO.isEnable();
        //这里把这两个变量加入到dynamicContext中上下文中

        //人群标签标识是空的 说明没有设置限制人数
        if(StringUtils.isBlank(tagId)){
            dynamicContext.setVisible(true);
            dynamicContext.setVisible(true);

            return router(requestParameter,dynamicContext);
        }

        //判断当前人是否在人群标签里面
        boolean isWithin=repository.isTagCrowRange(tagId,requestParameter.getUserId());

        //只要有一个条件不符合 都不会被过滤 都是可见的
        dynamicContext.setVisible(visible || isWithin);
        dynamicContext.setEnable(enable || isWithin);
        return router(requestParameter,dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {



        return endNode;
    }
}
